import * as echarts from '../../../ec-canvas/echarts.js';
const outSide = {
  rpx: '',
  // xInfo: [0, 0, 0, 0, 0],
  // yInfo: [0, 0, 0, 0, 0],
  xInfo: wx.getStorageSync('xInfo'),
  yInfo: wx.getStorageSync('yInfo'),
}
const app = getApp();
const time = require('../../../utils/util.js');

Page({
  data: {
    ecBar: {
      // 如果想要禁止触屏事件，以保证在图表区域内触摸移动仍能滚动页面，
      // 就将 disableTouch 设为 true
      // disableTouch: true,

      onInit: function(canvas, width, height) {
        console.log(375 * outSide.rpx)
        const lineChart = echarts.init(canvas, null, {
          width: 340 * outSide.rpx,
          height: 320 * outSide.rpx
        });
        canvas.setChart(lineChart);
        lineChart.setOption(getBarOption(wx.getStorageSync('xInfo'), wx.getStorageSync('yInfo')));

        return lineChart;
      }
    },
    //主要信息
    balance: [],
    code: '',
    extract_time: 0,
    receive_time: 0,

    //参数
    rpx: '',
    page: 1,

    //弹框
    flag_rule: true,
    flag_ruleTwo: true,

    flag_update: false,
  },
  // 是否要升级
  get_isupdate() {
    wx.request({
      url: app.globalData.url + 'api/broker/hand/upgrade/alert',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let updateInfo = e.data.data,
          flag_update = e.data.data.is_upgrade == 1 ? true : false;
        this.setData({
          updateInfo: updateInfo,
          flag_update: flag_update,
        })

      },
    })
  },
  //去升级
  go_update() {
    this.get_isupdate();
    wx.navigateTo({
      url: 'upgrade/upgrade'
    })
  },
  //七日收益
  earns() {
    wx.request({
      url: app.globalData.url + 'api/task/seven/day/earnings',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e) => {
        let earnsInfo = e.data.data.earnings;
        var xInfo = [],
          yInfo = [];
        earnsInfo.forEach(function(value, index) {
          var str = value.show_date;
          var str_new = str.replace(/"-"/, ".");
          var str_re = str_new.substring(5);
          value.show_date = str_re;
          value.operate_num = parseInt(value.operate_num);
          xInfo.push(value.show_date);
          yInfo.push(value.operate_num)
        })
        outSide.xInfo = xInfo;
        outSide.yInfo = yInfo;

        console.log(outSide.xInfo)
        console.log(outSide.yInfo)
      }
    })
  },
  getRpx() {
    wx.getSystemInfo({
      success: (e) => {
        var rpx = e.windowWidth / 375;
        outSide.rpx = rpx
        this.setData({
          rpx: rpx,
        })
      }
    })
  },
  //去任务
  go_task() {
    wx.navigateTo({
      url: '../task/index?show=show_task'
    })
  },
  //取得规则
  getRule() {
    wx.request({
      url: app.globalData.url + 'api/broker/lift/rule',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        let welfare = e.data.data.welfare,
          rule = e.data.data.rule,
          upgrade = e.data.data;
        this.setData({
          upgrade: upgrade,
        })
      },

    })
  },
  //去升级
  go_upgrade() {
    wx.navigateTo({
      url: 'upgrade/upgrade',
    })
  },
  //查看公司
  go_check() {
    wx.navigateTo({
      url: 'check/check',
    })
  },
  //滚动加载
  loadDetail: function(e) {
    let that = this;
    let page = this.data.page;
    page = page + 1;
    this.setData({
      page: page
    })
    this.List_invite();
  },

  // 邀请页码
  List_invite: function(e) {
    let that = this;
    let page = this.data.page,
      no_resume = this.data.no_resume;
    that.get_bills(page);
  },

  //邀请记录
  get_bills(page) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/user/price/list',
      data: {
        token: app.globalData.token,
        page: page
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        this.setData({
          total: e.data.data.list.last_page,
          current: e.data.data.list.current_page,
        })
        if (e.data.data.list.last_page > e.data.data.list.total) {
          console.log("到底了");
          wx.showToast({
            title: '到底啦~',
            icon: 'none',
          })
        } else {
          if (this.data.page == 1) {
            let bills = e.data.data.list.data;
            bills.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'Y.M.D h:m');
            })
            this.setData({
              bills: bills,
            })
          } else {
            // console.log("不不不不不");
            let bills = that.data.bills;
            let _bills = e.data.data.list.data;
            _bills.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'Y.M.D h:m');
            })
            bills.push(..._bills);
            //   console.log(CollectList);
            this.setData({
              bills: bills,
            })
          }
          wx.hideLoading();
        }
      },
    })
  },

  //隐藏弹框
  modal_hide() {
    this.setData({
      flag_share: true,
      flag_update: false,
    })
  },
  //加载个人信息
  getInfo() {
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let info = e.data.data,
          realMoney = parseInt(info.bb) ? parseInt(info.bb) / this.data.balance.bb_rate : 0;
        this.setData({
          info: info,
          realMoney: realMoney,
        })
      },
    })
  },
  //查看体现详情
  go_detail() {
    let that = this;
    wx.navigateTo({
      url: '../welfareDetail/welfareDetail',
    })
  },
  //显示规则
  click_showrule() {
    this.setData({
      flag_rule: false,
    })
  },
  //隐藏
  click_hide() {
    this.setData({
      flag_rule: true,
      flag_ruleTwo: true,
    })
  },
  //点击 提现
  change_withdraw() {
    wx.request({
      url: app.globalData.url + 'api/invitation/apply/condition',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let type = e.data.data.type;
        this.setData({
          type: type,
        })
        if (e.data.data.type == 0) {
          wx.navigateTo({
            url: 'anchorWelfare_detail/anchorWelfare_detail',
          })
        } else if ((e.data.data.type == 2)) {
          wx.showToast({
            title: '简历审核中',
            icon: 'none',
          })
        } else if ((e.data.data.type == 7)) {
          wx.showToast({
            title: '提现审核中',
            icon: 'none',
          })
        } else {
          this.setData({
            flag_ruleTwo: false,
          })
        }
      },
    })
  },
  //复制
  click_copy() {
    let code = this.data.balance.code;
    wx.setClipboardData({
      data: code,
    })
  },
  //获取我的额度 等信息
  balance() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/my/balance',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let balance = e.data.data;
        let code = e.data.data.code;
        that.setData({
          balance: balance,
          code: code,
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getRpx();
    this.earns();
    this.get_bills();
    this.balance();
    this.getInfo();
    this.getRule();
    this.get_isupdate();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.get_isupdate();
  }
});

function getBarOption(_xInfo, _yInfo) {
  return {
    color: ["#d0a76c"],
    grid: {
      x: '0', //相当于距离左边效果:padding-left
      y: '10%', //相当于距离上边效果:padding-top
      // x2: '10',
      // y2: '10',
      bottom: '30%',
      containLabel: true,
    },
    tooltip: {
      show: true,
      trigger: 'axis',
      // confine:true,
      position: ['10%', 0],
      axisPointer: {
        type: 'line',
        lineStyle: {
          color: '#d0a76c',
        },
        textStyle: {
          color: '#d0a76c',
        }
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      nameGap: 0,
      data: _xInfo,
      axisLine: {
        lineStyle: {
          color: "#dedede"
        },
        show: false
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        fontSize: 10,
      }
    },
    yAxis: {
      x: 'center',
      type: 'value',
      show: true,
      splitLine: {
        lineStyle: {
          type: 'solid',
          color: 'rgba(0,0,0,.1)'
        }
      },
      axisLine: {
        lineStyle: {
          color: "#dedede"
        },
        show: false
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        fontSize: 10,
      },
    },
    series: [{
      name: '收益',
      type: 'line',
      areaStyle: {
        opacity: .1,
      },
      smooth: false,
      data: _yInfo,
      itemStyle: {
        normal: {
          label: {
            show: false
          }
        }
      },
      lineStyle: {
        width: 1
      }
    }]
  };
}